<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Recipe_model extends OC_Model {

	public function get_items($limit,$offset,$cat_id)
	{
		$this->db->select('a.*,b.cr_name,b.cr_id');
		$this->db->from('recipes a');
		$this->db->join('category_recipes b', 'b.cr_id = a.cr_id', 'left');
		$this->db->order_by('a.cr_id', 'desc');
		if(!empty($cat_id)) $this->db->where('a.cr_id', $cat_id);
		if(!empty($limit)) $this->db->limit($limit,$offset);
		return $this->db->get()->result();
	}


	public function count($limit=false,$offset=false,$cat_id=false)
	{
		return count($this->get_items($limit,$offset,$cat_id));
	}


	public function get_categories()
	{
		$this->db->order_by('cr_id', 'Asc');
		$this->db->where('cr_active', 1);
		return $this->db->get('category_recipes')->result();
	}


	public function get_item_details($id=false){
		$this->db->select('a.*,b.cr_name,b.cr_id');
		$this->db->from('recipes a');
		$this->db->join('category_recipes b', 'b.cr_id = a.cr_id', 'left');
		$this->db->where('a.r_id', $id);
		return $this->db->get()->row_array();
	}

	public function item_per_category($cat,$limit,$offset)
	{
		$this->db->select('a.*,b.cr_name,b.cr_id');
		$this->db->from('recipes a');
		$this->db->join('category_recipes b', 'b.cr_id = a.cr_id', 'left');
		$this->db->where('a.cr_id', $cat);
		if(!empty($limit)) $this->db->limit($limit,$offset);
		return $this->db->get()->result();
	}

	public function get_meta($meta=false,$id=false)
	{
		$this->db->select('r_meta_'.$meta);
		$this->db->where('r_id', $id);
		$meta_data = $this->db->get('recipes')->row_array();
		return $meta_data['r_meta_'.$meta];
	}

	public function get_result_recipes($keyword)
	{
		$this->db->like('r_name', $keyword);
		$this->db->or_like('r_desc',$keyword);
		return $this->db->get('recipes')->result();
	}

	public function do_comment($object)
	{
		return $this->db->insert('comment_recipes', $object);
	}

	public function get_comments($id=false)
	{
		$this->db->where('r_id', $id);
		$this->db->where('cr_approved', 1);
		return $this->db->get('comment_recipes')->result();
	}

	public function do_rating($data)
	{
		return $this->db->insert('recipes_ratings', $data);
	}

	public function check_user_ip($user_ip,$id)
	{
		$this->db->select('users_ip');
		$this->db->where('r_id', $id);
		$this->db->where('users_ip', $user_ip);
		$uip = $this->db->get('recipes_ratings')->num_rows();
		if($uip>0)
			return false;
		else
			return true;
	}

	private function _sum_rating($id)
	{
		$this->db->select('sum(rating) as rating');
		$this->db->where('r_id', $id);
		return $this->db->get('recipes_ratings')->row_array();
	}

	private function _get_rating($id)
	{
		$sr = $this->_sum_rating($id);
		$this->db->select('rating');
		$this->db->where('id', $id);
		$rows = $this->db->get('recipes_ratings')->num_rows();
		if($rows > 0)
		{
			$count = floor($sr['rating']/$rows);
		}else{
			$count = 0;
		}
		return $count;

	}


	public function show_rating($id)
	{
		$rating =  $this->_get_rating($id);

		switch ($rating) {
			case $rating==0:
				return '<i class="fa fa-star-o"></i>
					<i class="fa fa-star-o"></i>
					<i class="fa fa-star-o"></i>
					<i class="fa fa-star-o"></i>
					<i class="fa fa-star-o"></i>';
				break;

			case $rating<=10:
				return '<i class="fa fa-star-half-o"></i>
					<i class="fa fa-star-o"></i>
					<i class="fa fa-star-o"></i>
					<i class="fa fa-star-o"></i>
					<i class="fa fa-star-o"></i>';
				break;

			case $rating<=20:
				return '<i class="fa fa-star"></i>
					<i class="fa fa-star-o"></i>
					<i class="fa fa-star-o"></i>
					<i class="fa fa-star-o"></i>
					<i class="fa fa-star-o"></i>';
				break;
			
			case $rating<=30:
				return '<i class="fa fa-star"></i>
					<i class="fa fa-star-half-o"></i>
					<i class="fa fa-star-o"></i>
					<i class="fa fa-star-o"></i>
					<i class="fa fa-star-o"></i>';
				break;

			case $rating<=40:
				return '<i class="fa fa-star"></i>
					<i class="fa fa-star"></i>
					<i class="fa fa-star-o"></i>
					<i class="fa fa-star-o"></i>
					<i class="fa fa-star-o"></i>';
				break;

			case $rating<=50:
				return '<i class="fa fa-star"></i>
					<i class="fa fa-star"></i>
					<i class="fa fa-star-half-o"></i>
					<i class="fa fa-star-o"></i>
					<i class="fa fa-star-o"></i>';
				break;

			case $rating<=60:
				return '<i class="fa fa-star"></i>
					<i class="fa fa-star"></i>
					<i class="fa fa-star"></i>
					<i class="fa fa-star-o"></i>
					<i class="fa fa-star-o"></i>';
				break;

			case $rating<=70:
				return '<i class="fa fa-star"></i>
					<i class="fa fa-star"></i>
					<i class="fa fa-star"></i>
					<i class="fa fa-star-half-o"></i>
					<i class="fa fa-star-o"></i>';
				break;

			case $rating<=80:
				return '<i class="fa fa-star"></i>
					<i class="fa fa-star"></i>
					<i class="fa fa-star"></i>
					<i class="fa fa-star"></i>
					<i class="fa fa-star-o"></i>';
				break;

			case $rating<=90:
				return '<i class="fa fa-star"></i>
					<i class="fa fa-star"></i>
					<i class="fa fa-star"></i>
					<i class="fa fa-star"></i>
					<i class="fa fa-star-half-o"></i>';
				break;

			case $rating<=100:
				return '<i class="fa fa-star"></i>
					<i class="fa fa-star"></i>
					<i class="fa fa-star"></i>
					<i class="fa fa-star"></i>
					<i class="fa fa-star"></i>';
				break;

			default:
				return '<i class="fa fa-star-half-o"></i>
					<i class="fa fa-star-o"></i>
					<i class="fa fa-star-o"></i>
					<i class="fa fa-star-o"></i>
					<i class="fa fa-star-o"></i>';
				break;
		}
	}

}

/* End of file recipe_model.php */
/* Location: ./applications/module_front/recipes/models/recipe_model.php */